/*!
 @header DealManagementViewController.m
 @author 王 文彬
 @version 1.0.0 2013/01/24 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "DealManagementViewController.h"
#import "Config.h"
#import "Helper.h"
#import "ConsigneeListViewController.h"
#import "OrderManagementViewController.h"
#import "ProductListViewController.h"
#import "RTLabel.h"
#import "JMFBase.h"


//  -----------------------类详细介绍-----------------------
//
//  <功能描述>：DealManagementViewController功能较为简单，在背景上排列多个Label，请求基础数据并显示。点击区域用按钮来实现。
//
//  -----------------------类升级日志-----------------------
//
//  修改人   修改时间        修改描述              版本
//
// <#修改人#> <#修改时间#>    <#修改描述#>          <#版本#>


@interface DealManagementViewController ()

@end

@implementation DealManagementViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = _L(@"vc_title_012");   //交易管理
    
    //卷动视图
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, _SYS_FRAME_HEIGHT - 44)];
    //内容高度
    scrollView.contentSize = CGSizeMake(_SYS_FRAME_WIDTH, 510);
    //背景
    scrollView.backgroundColor = [UIColor whiteColor];
    
    scrollView.scrollEnabled = NO;
    [self.view addSubview:scrollView];
    
    
    //有透明的灰色图标的背景图层。不能置于scrollView上，因为卷动时，背景会反复出现
    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 460 - 44 - 42)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"transaction_img_009@2x" ofType:@"png"]]];
    
    [scrollView addSubview:bgView];


    //产品管理数据label
    _productLabel1 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 55, 300, 70)];
    _productLabel1.font = _SYS_FONT_004;
    _productLabel1.textColor = _SYS_COLOR_002;
    _productLabel1.text = [NSString stringWithFormat:_L(@"tpl_19"), 0];
    [scrollView addSubview:_productLabel1];
    
    _productLabel2 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 75, 300, 70)];
    _productLabel2.font = _SYS_FONT_004;
    _productLabel2.textColor = _SYS_COLOR_002;
    _productLabel2.text = [NSString stringWithFormat:_L(@"tpl_20"), 0];
    [scrollView addSubview:_productLabel2];
    
    _productLabel3 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 95, 300, 70)];
    _productLabel3.font = _SYS_FONT_004;
    _productLabel3.textColor = _SYS_COLOR_002;
    _productLabel3.text = [NSString stringWithFormat:_L(@"tpl_21"), 0];
    [scrollView addSubview:_productLabel3];

    
    //按钮
    UIButton* productButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 125)];
    [productButton addTarget:self action:@selector(productButtonEvent) forControlEvents:UIControlEventTouchUpInside];

    [scrollView addSubview:productButton];
    
    
    //订单管理数据label
    _orderLabel1 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 175, 300, 20)];
    _orderLabel1.font = _SYS_FONT_004;
    _orderLabel1.textColor = _SYS_COLOR_002;
    _orderLabel1.text = [NSString stringWithFormat:_L(@"tpl_13"), 0];
    [scrollView addSubview:_orderLabel1];
    
    _orderLabel2 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 195, 300, 20)];
    _orderLabel2.font = _SYS_FONT_004;
    _orderLabel2.textColor = _SYS_COLOR_002;
    _orderLabel2.text = [NSString stringWithFormat:_L(@"tpl_14"), 0];
    [scrollView addSubview:_orderLabel2];
    
    _orderLabel3 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 215, 300, 20)];
    _orderLabel3.font = _SYS_FONT_004;
    _orderLabel3.textColor = _SYS_COLOR_002;
    _orderLabel3.text = [NSString stringWithFormat:_L(@"tpl_15"), 0];
    [scrollView addSubview:_orderLabel3];
    
    _orderLabel4 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 235, 300, 20)];
    _orderLabel4.font = _SYS_FONT_004;
    _orderLabel4.textColor = _SYS_COLOR_002;
    _orderLabel4.text = [NSString stringWithFormat:_L(@"tpl_16"), 0];
    [scrollView addSubview:_orderLabel4];
    
    _orderLabel5 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 255, 300, 20)];
    _orderLabel5.font = _SYS_FONT_004;
    _orderLabel5.textColor = _SYS_COLOR_002;
    _orderLabel5.text = [NSString stringWithFormat:_L(@"tpl_17"), 0];
    [scrollView addSubview:_orderLabel5];
    
    _orderLabel6 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 275, 300, 20)];
    _orderLabel6.font = _SYS_FONT_004;
    _orderLabel6.textColor = _SYS_COLOR_002;
    _orderLabel6.text = [NSString stringWithFormat:_L(@"tpl_18"), 0];
    [scrollView addSubview:_orderLabel6];
    
    //按钮
    UIButton* orderButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 126, _SYS_FRAME_WIDTH, 182)];
    [orderButton addTarget:self action:@selector(orderButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:orderButton];
    
    
    //地址管理
    UIButton* consigneeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 309, _SYS_FRAME_WIDTH, 120)];
    [consigneeButton addTarget:self action:@selector(consigneeButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [scrollView addSubview:consigneeButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


// 视图将要载入时，刷新数据
//
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_PRODUCT_TRANSCATION_STATISTICS];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_02");
    [request asynchronous];
}

#pragma mark - 自定义事件

/*!
 @abstract 产品管理按钮事件
 */
- (void)productButtonEvent
{
    ProductListViewController* productListViewController = [[ProductListViewController alloc] init];
    [productListViewController setHidesBottomBarWhenPushed:true];
    
    [self.navigationController pushViewController:productListViewController animated:true];
}

/*!
 @abstract 订单管理按钮事件
 */
- (void)orderButtonEvent
{
    OrderManagementViewController* orderManagementViewController = [[OrderManagementViewController alloc] init];
    [orderManagementViewController setHidesBottomBarWhenPushed:true];
    
    [self.navigationController pushViewController:orderManagementViewController animated:true];
}

/*!
 @abstract 地址管理按钮事件
 */
- (void)consigneeButtonEvent
{
    ConsigneeListViewController* consigneeListViewController = [[ConsigneeListViewController alloc] init];
    [consigneeListViewController setHidesBottomBarWhenPushed:true];
    
    [self.navigationController pushViewController:consigneeListViewController animated:true];
}


#pragma mark - JSONDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        NSDictionary* values = [dict valueForKey:@"data"];
        
        _productLabel1.text = [NSString stringWithFormat:_L(@"tpl_19"), [values[@"productCount"] integerValue]];
        _productLabel2.text = [NSString stringWithFormat:_L(@"tpl_20"), [values[@"productSellCount"] integerValue]];
        _productLabel3.text = [NSString stringWithFormat:_L(@"tpl_21"), [values[@"productStopSellCount"] integerValue]];
        
        _orderLabel1.text = [NSString stringWithFormat:_L(@"tpl_13"), [values[@"waitPayCount"] integerValue]];
        _orderLabel2.text = [NSString stringWithFormat:_L(@"tpl_14"), [values[@"waitDeliveryCount"] integerValue]];
        _orderLabel3.text = [NSString stringWithFormat:_L(@"tpl_15"), [values[@"waitReceivingCount"] integerValue]];
        _orderLabel4.text = [NSString stringWithFormat:_L(@"tpl_16"), [values[@"historySuccessCount"] integerValue]];
        _orderLabel5.text = [NSString stringWithFormat:_L(@"tpl_17"), [values[@"refundCount"] intValue]];
        _orderLabel6.text = [NSString stringWithFormat:_L(@"tpl_18"), [values[@"returnCount"] intValue]];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

@end
